<template>
  <div class="pagination">
    <button :disabled="pageNo == 1" @click="$emit('getUserPageNo', pageNo - 1)">
      上一页
    </button>
    <button
      v-if="startNumAndEndNum.start > 1"
      @click="$emit('getUserPageNo', 1)"
      :class="{active:pageNo==1}"
    >
      1
    </button>
    <button v-if="startNumAndEndNum.start > 2">···</button>

    <!-- 中间部分 -->
    <button
      v-for="(page, index) in startNumAndEndNum.end"
      :key="index"
      v-show="page >= startNumAndEndNum.start"
      @click="$emit('getUserPageNo', page)"
      :class="{active:page==pageNo}"
    >
      {{ page }}
    </button>

    <button v-if="startNumAndEndNum.end < totalPages - 1">···</button>
    <button
      v-if="startNumAndEndNum.end < totalPages"
      @click="$emit('getUserPageNo', totalPages)"
      :class="{active:totalPages==pageNo}"
    >
      {{ totalPages }}
    </button>
    <button
      :disabled="pageNo == totalPages"
      @click="$emit('getUserPageNo', pageNo + 1)"
     
    >
      下一页
    </button>

    <button style="margin-left: 30px">共 {{ total }} 条</button>
  </div>
</template>
  
  <script>
export default {
  name: "Pagination",
  props: ["total", "totalPages", "continues", "pageSize", "pageNo"],
  //total 总条数 totalPages总页数 continues连续分页数  pageNo当前页码
  data() {
    return {
      start: 0,
      end: 0,
    };
  },
  computed: {
    startNumAndEndNum() {
      const { continues, totalPages, pageNo } = this;
      //中间部分计算
      let start = 0;
      let end = 0;
      //正常情况下 连续分页数小于总页数
      if (totalPages < continues) {
        start = 1;
        end = totalPages;
      } else {
        start = pageNo - parseInt(continues / 2);
        end = pageNo + parseInt(continues / 2);

        //如果当前页码减去了contius对应的数值小于1  也就是0 -1 -2等不存在的页码
        if (start < 1) {
          start = 1;
          end = continues;
        }
        //如果当前页码的
        if (end > totalPages) {
          end = totalPages;
          start = totalPages - continues + 1;
        }

        // end=pageNo+parseInt(continues/2)
      }

      return { start, end };
    },
  },
};
</script>
  
  <style lang="less" scoped>
.pagination {
  text-align: center;
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 20px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 40px;
    line-height: 40px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;


  }
}

.active {
  background-color: #409eff!important;
  color: #fff!important;
}
</style>